﻿//
// GNS_PvEAllDate.cpp
// MobileManageSys
//FuncTion  人机对战总玩数据
// Created by mr.l on 16/1/16.
//
//
#include "GnsClientFrame.h"
#include "GNS_PvEAllDate.h"
#include "cocostudio/CocoStudio.h"
#include "ui/CocosGUI.h"
#include "GNS_PlayerMgr.h"
#include "GNS_Common.h"
USING_NS_CC;
using namespace cocostudio::timeline;
using namespace ui;

#define winsize Director::getInstance()->getVisibleSize()
////extern ISocketMgr * _GameSock;

GNS_PvEAllDate::GNS_PvEAllDate()
{
    pGNS_PvEAllDate=0;
    pPveAllDateDaily=nullptr;
    pCalendar=nullptr;
    AllDate=nullptr;
    agentbg =nullptr;
    promotersbg=nullptr;
    Playerbg=nullptr;
    Allincome=nullptr;
    AllPlayout=nullptr;
    Allprofit=nullptr;
    TimeText=nullptr;
    GameName =nullptr;
    Showtype = nullptr;
    NodeTitle = nullptr;
    TypeListView = nullptr;
    DateListView=nullptr;
    allinfo=nullptr;
    allinto=nullptr;
    gameprofit=nullptr;
    separator=nullptr;
    typeNum=0;
    SendType=0;
    Mon=0;
    Year=0;
    Day=0;
    GameType=nullptr;
}

GNS_PvEAllDate::~GNS_PvEAllDate()
{
    
}

GNS_PvEAllDate* GNS_PvEAllDate::createLayer()
{
    auto pGNS_PvEAllDate = GNS_PvEAllDate::create();
    auto pScene = Director::getInstance()->getRunningScene();
    if (!pScene)
        return 0;
    pScene->addChild(pGNS_PvEAllDate);
    return pGNS_PvEAllDate;
}

bool GNS_PvEAllDate::init()
{
    if ( !Layer::init() )
    {
        return false;
    }
    /**
     *  @author MR.L, 16-02-16 11:02:50
     *
     *  获取当前时间
     */
    time_t tt;
    time(&tt);
    struct tm * time;
    time = localtime(&tt);
    
    Mon = (int)time->tm_mon+1;
    Year = time->tm_year+1900;
    Day  =(int)time->tm_mday;
    
	pPveAllDateDaily=GNS_PVEAllDateDaily::createLayer();
	pPveAllDateDaily->setVisible(false);
    return true;
}
void GNS_PvEAllDate::SetNodeName(std::string CsbName)
{
    pGNS_PvEAllDate = CSLoader::createNode(CsbName);
    pGNS_PvEAllDate->setContentSize(winsize);
    ui::Helper::doLayout(pGNS_PvEAllDate);
    addChild(pGNS_PvEAllDate,11);
    GNS_PlayerMgr::getInstance()->_NodeChildVec.pushBack(pGNS_PvEAllDate);
    auto pBackBtn = static_cast<Button*>(pGNS_PvEAllDate->getChildByName("PvEAllDateBtn"));
    pBackBtn->setTag(1);
    pBackBtn->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllDate::BtnCallBack, this));
    
    auto pTimeBtn = static_cast<Button*>(pGNS_PvEAllDate->getChildByName("Button_3"));
    pTimeBtn->setTag(2);
    pTimeBtn->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllDate::BtnCallBack, this));
    
    auto pCombined = static_cast<Button *>(pGNS_PvEAllDate->getChildByName("combined"));
    pCombined->setTag(3);
    pCombined->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllDate::BtnCallBack, this));
    
    
    auto pTimeSer = static_cast<Button*>(pGNS_PvEAllDate->getChildByName("Button_8"));
    pTimeSer->setTag(2);
    pTimeSer->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllDate::BtnCallBack, this));
    
    auto typrbtnSrt = static_cast<Button*>(pGNS_PvEAllDate->getChildByName("Button_8_0"));
    typrbtnSrt->setTag(3);
    typrbtnSrt->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllDate::BtnCallBack, this));
    
    auto gamebtn = static_cast<Button*>(pGNS_PvEAllDate->getChildByName("Button_8_1"));
    gamebtn->setTag(4);
    gamebtn->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllDate::BtnCallBack, this));
    
    auto gamebtn1 = static_cast<Button*>(pGNS_PvEAllDate->getChildByName("Button_3_0"));
    gamebtn1->setTag(4);
    gamebtn1->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllDate::BtnCallBack, this));
    
    TypeListView = static_cast<ListView*>(pGNS_PvEAllDate->getChildByName("TypeListView"));
    TypeListView->setVisible(false);
    TypeListView->setZOrder(13);
    DateListView = static_cast<ListView*>(pGNS_PvEAllDate->getChildByName("DateListView"));
    DateListView->removeAllItems();
    DateListView->setVisible(false);

    GameName = static_cast<Text*>(pGNS_PvEAllDate->getChildByName("GameName"));
    GameType = static_cast<ListView*>(pGNS_PvEAllDate->getChildByName("GameType"));
    GameType->removeAllItems();
   
	int x = 0;
    for (auto it:(* GNS_PlayerMgr::getInstance()->getGameList())) {
        if (it.iGameType==1) {
            auto pbutton = Button::create("p0Btn.png");
			pbutton->setTitleText(SystemMgr::getInstance()->GbToUTF8(it.szGameName));
            pbutton->setTitleColor(Color3B::BLACK);
            pbutton->setTitleFontSize(20);
            pbutton->setTag(10);
            pbutton->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllDate::BtnCallBack, this));
            GameType->addChild(pbutton);
			++x;
			if (x == 1)
				GameName->setString(SystemMgr::getInstance()->GbToUTF8(it.szGameName));
        }
    }
    GameType->setVisible(false);

    auto TypeAllBtn = static_cast<Button*>(TypeListView->getChildByName("Allbtn"));
    TypeAllBtn->setTag(5);
    TypeAllBtn->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllDate::ListViewCallBack, this));
    
    auto TypeAgentBtn = static_cast<Button*>(TypeListView->getChildByName("AgentBtn"));
    TypeAgentBtn->setTag(6);
    TypeAgentBtn->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllDate::ListViewCallBack, this));
    
    auto TypePromoterBtn = static_cast<Button*>(TypeListView->getChildByName("PromoterBtn"));
    TypePromoterBtn->setTag(7);
    TypePromoterBtn->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllDate::ListViewCallBack, this));
    
    auto TypePlayerBtn = static_cast<Button*>(TypeListView->getChildByName("PlayerBtn"));
    TypePlayerBtn->setTag(8);
    TypePlayerBtn->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllDate::ListViewCallBack, this));
    
    NodeTitle = static_cast<Text*>(pGNS_PvEAllDate->getChildByName("Text_5"));
    
    //UI分类
    TimeText = static_cast<Text *>(pGNS_PvEAllDate->getChildByName("Text_3"));
    TimeText->setString(StringUtils::format("%d-%02d-%02d",Year,Mon,Day));

//    if (GNS_PlayerMgr::getInstance()->getGameList()->size()!=0) {
//        GameName->setString(GNS_PlayerMgr::getInstance()->getGameList()->at(0).szGameName);
//    }
    Showtype = static_cast<Text *>(pGNS_PvEAllDate->getChildByName("type"));
    
    
    
    //UI标题
    AllDate = static_cast<ImageView *>(pGNS_PvEAllDate->getChildByName("alldate"));
    agentbg =static_cast<ImageView *>(pGNS_PvEAllDate->getChildByName("agentbg"));
    agentbg->setVisible(false);
    promotersbg=static_cast<ImageView *>(pGNS_PvEAllDate->getChildByName("promotersbg"));
    promotersbg->setVisible(false);
    Playerbg=static_cast<ImageView *>(pGNS_PvEAllDate->getChildByName("Playerbg"));
    Playerbg->setVisible(false);
    TitleVec.push_back(AllDate);
    TitleVec.push_back(agentbg);
    TitleVec.push_back(promotersbg);
    TitleVec.push_back(Playerbg);
    
    allinfo = static_cast<Text*>(pGNS_PvEAllDate->getChildByName("Text_9"));
    allinto = static_cast<Text*>(pGNS_PvEAllDate->getChildByName("Text_9_0"));
    gameprofit = static_cast<Text*>(pGNS_PvEAllDate->getChildByName("Text_9_1"));
    separator = static_cast<ImageView*>(pGNS_PvEAllDate->getChildByName("Image_11"));
    
    typeNum=GNS_PlayerMgr::getInstance()->getUserLevel();
    if (typeNum==0) {
        TypeListView->removeChild(TypePromoterBtn);
        TypeListView->removeChild(TypePlayerBtn);
    }else if (typeNum==1)
    {
        TypeListView->removeChild(TypeAgentBtn);
        TypeListView->removeChild(TypePlayerBtn);
    }else if (typeNum==2)
    {
        SendType=3;
        TypeListView->removeChild(TypePromoterBtn);
        TypeListView->removeChild(TypeAgentBtn);
        TypeListView->removeChild(TypeAllBtn);
        allinfo->setVisible(false);
        allinto->setVisible(false);
        gameprofit->setVisible(false);
        separator->setVisible(false);
        DateListView->setVisible(true);
        Playerbg->setVisible(true);
        AllDate->setVisible(false);
        Showtype->setString(TypePlayerBtn->getTitleText());

    }
    
}
void GNS_PvEAllDate::BtnCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type){
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();
        switch (BtnTag) {
            case 1:
            {
                if (GNS_PlayerMgr::getInstance()->_NodeChildVec.size()!=0) {
                    GNS_PlayerMgr::getInstance()->_NodeChildVec.clear();
                    this->removeAllChildrenWithCleanup(true);
                    this->getParent()->removeChild(this);
                }
                //auto RevRepNode=GNS_PlayerMgr::getInstance()->_NodeHallVec.at(3);
                //RevRepNode->setVisible(true);

				GNS_NodeMgr::getInstance()->setMenuVisible("RevRep", true);
                break;
            }
            case 2:
            {
                /*pCalendar =GNS_Calendar::createCalendar();
                pCalendar->SetCalendBgImage("Calendarbg.png");
                std::vector<Button *> CalenderBtn;
                pCalendar->GetTimeButton()->swap(CalenderBtn);
                for (auto & it:CalenderBtn) {
                    it->setTag(100);
                    it->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllDate::TimeButtonCallBack, this));
                }*/

				pCalendar = GNS_Calendar::createCalendar();
				pCalendar->setButtonsCallback(CC_CALLBACK_2(GNS_PvEAllDate::TimeButtonCallBack, this));
                break;
            }
            case 3:
            {
                if (TypeListView->isVisible()==true) {
                    TypeListView->setVisible(false);
                }else{
                    TypeListView->setVisible(true);
                }
                break;
            }
            case 4:
            {
                if (GameType->isVisible()==true) {
                    GameType->setVisible(false);
                }else{
                    GameType->setVisible(true);
                }
                break;
            }
            case 10:
            {
                log("%s",pButton->getTitleText().c_str());
                GameName->setString(pButton->getTitleText());
                for(int i=0;i<GNS_PlayerMgr::getInstance()->getGameList()->size();i++)
                {
					auto st1 = SystemMgr::getInstance()->utf8ToGb(pButton->getTitleText().c_str());
					auto st2 = GNS_PlayerMgr::getInstance()->getGameList()->at(i).szGameName;
                    if (st1 == st2) {
                        GNS_PlayerMgr::getInstance()->getPlayProFit()->clear();
						GNS_PlayerMgr::getInstance()->getPlayProPlayFit()->clear();
                        CMD_CS_GetManToMachPlayProfit pGetPlayPro;
                        pGetPlayPro._agentCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
                        pGetPlayPro._uplineCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
                        pGetPlayPro._gameId=GNS_PlayerMgr::getInstance()->getGameList()->at(i).nGameId;
                        pGetPlayPro._type=SendType;
                        std::string TimeDate = StringUtils::format("%d-%02d-%02d",Year,Mon,Day);
                        memcpy(pGetPlayPro._selDate, TimeDate.c_str(), 11);
                        GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_MANTOMACH_GAME_PROFIT, (char*)&pGetPlayPro, sizeof(pGetPlayPro));
						GNS_PlayerMgr::getInstance()->setGameId(pGetPlayPro._gameId);

						GameType->setVisible(false);
                    }
                }
                break;
            }
            default:
                break;
        }
    }
}

void GNS_PvEAllDate::TimeButtonCallBack(cocos2d::Ref *pSender, ui::Widget::TouchEventType type)
{
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        std::string BtnText = pButton->getTitleText();
        Mon = pCalendar->Mon;
        Year = pCalendar->year;
        Day = atoi(BtnText.c_str());
        TimeText->setString(StringUtils::format("%d-%02d-%02d",Year,Mon,atoi(BtnText.c_str())));
        pCalendar->Show(false);
        
        GNS_PlayerMgr::getInstance()->getPlayProFit()->clear();
        CMD_CS_GetManToMachPlayProfit pGetPlayPro;
        pGetPlayPro._agentCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
        pGetPlayPro._uplineCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
        pGetPlayPro._gameId=GNS_PlayerMgr::getInstance()->getGameId();
        pGetPlayPro._type=SendType;
        std::string TimeDate = StringUtils::format("%d-%02d-%02d",Year,Mon,Day);
        memcpy(pGetPlayPro._selDate, TimeDate.c_str(), 11);
        GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_MANTOMACH_GAME_PROFIT, (char*)&pGetPlayPro, sizeof(pGetPlayPro));
    }
    
}

void GNS_PvEAllDate::ListViewCallBack(Ref*pSender,ui::Widget::TouchEventType type)
{
    if (type==ui::Widget::TouchEventType::ENDED) {
        auto pButton = (Button*)pSender;
        int BtnTag = pButton->getTag();
        std::string btntext = pButton->getTitleText();
        Showtype->setString(btntext);
        TypeListView->setVisible(false);
        switch (BtnTag) {
            case 5:
            {
                TitleChooseType(AllDate);
                SendType=0;
                break;
            }
            case 6:
            {
                TitleChooseType(agentbg);
                SendType=1;
                break;
            }
            case 7:
            {
                TitleChooseType(promotersbg);
                SendType=2;
                break;
            }
            case 8:
            {
                TitleChooseType(Playerbg);
                SendType=3;
                break;
            }
            default:
                break;
        }
        GNS_PlayerMgr::getInstance()->getPlayProFit()->clear();
        CMD_CS_GetManToMachPlayProfit pGetPlayPro;
        pGetPlayPro._agentCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
        pGetPlayPro._uplineCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
        pGetPlayPro._gameId=GNS_PlayerMgr::getInstance()->getGameId();
        pGetPlayPro._type=SendType;
        std::string TimeDate = StringUtils::format("%d-%02d-%02d",Year,Mon,Day);
        memcpy(pGetPlayPro._selDate, TimeDate.c_str(), 11);
        GnsClientFrame::getInstance()->sendMsg(0,MAIN_MSG_LOGIN, SUB_CS_GET_MANTOMACH_GAME_PROFIT, (char*)&pGetPlayPro, sizeof(pGetPlayPro));
    }
}
void GNS_PvEAllDate::TitleChooseType(cocos2d::ui::ImageView *type)
{
    log("TitleVec.size()==%lu",TitleVec.size());
    for (auto &it:TitleVec) {
        if (it==type) {
            it->setVisible(true);
        }else{
            it->setVisible(false);
        }
    }
    if(type==AllDate){
        allinfo->setVisible(true);
        allinto->setVisible(true);
        gameprofit->setVisible(true);
        separator->setVisible(true);
        DateListView->setVisible(false);
    }else{
        allinfo->setVisible(false);
        allinto->setVisible(false);
        gameprofit->setVisible(false);
        separator->setVisible(false);
        DateListView->setVisible(true);
    }

}

void GNS_PvEAllDate::AcceptSeverDate()
{
    if (SendType==0) {
        for (auto it:(*GNS_PlayerMgr::getInstance()->getPlayProFit())) {
            allinfo->setString(StringUtils::format("%lld",it._gamePlayIn));
            allinto->setString(StringUtils::format("%lld",it._gamePlayOut));
            gameprofit->setString(StringUtils::format("%lld",it._gameProfit));
        }
    }else{
        DateListView->removeAllItems();
        int index =(int)GNS_PlayerMgr::getInstance()->getPlayProFit()->size();
        int x= 0;
        Size bgSize;
        for (auto it:(*GNS_PlayerMgr::getInstance()->getPlayProFit())) {
            Button * itembg;
            itembg= Button::create("line between1.png");
            if (SendType==3) {
                itembg= Button::create("line2.png");
            }
            itembg->setTag(100+x);
            DateListView->addChild(itembg);
            itembg->addTouchEventListener(CC_CALLBACK_2(GNS_PvEAllDate::ListViewBtn, this));
            bgSize = itembg->getContentSize();
            
            auto Name = Text::create(StringUtils::format("%d",it._agentCode), GameFontName, 24);
            Name->setPosition(Vec2(bgSize.width*0.14, bgSize.height*0.4));
            Name->setColor(Color3B::BLACK);
            itembg->addChild(Name);
            
            auto Allinfo = Text::create(StringUtils::format("%lld",it._gamePlayIn), GameFontName, 24);
            Allinfo->setPosition(Vec2(bgSize.width*0.31, bgSize.height*0.4));
            Allinfo->setColor(Color3B::BLACK);
            itembg->addChild(Allinfo);
            
            auto Allinto = Text::create(StringUtils::format("%lld",it._gamePlayOut), GameFontName, 24);
            if (it._gamePlayOut == 0) {
                Allinto = Text::create(StringUtils::format("%s","暂无数据"), GameFontName, 24);
            }
            Allinto->setPosition(Vec2(bgSize.width*0.51, bgSize.height*0.4));
            Allinto->setColor(Color3B::BLACK);
            itembg->addChild(Allinto);
            
            auto AgameProfit = Text::create(StringUtils::format("%lld",it._gameProfit), GameFontName, 24);
            AgameProfit->setPosition(Vec2(bgSize.width*0.76, bgSize.height*0.4));
            AgameProfit->setColor(Color3B::BLACK);
            itembg->addChild(AgameProfit);
            x++;
        }
        if (bgSize.width!=0||bgSize.height!=0) {
            DateListView->setInnerContainerSize(Size(bgSize.width, (bgSize.height+10)*index));
        }
    }
}
void GNS_PvEAllDate::SelectedItemEvents(cocos2d::Ref *pSender, ui::ListView::EventType type)
{
//    if (type==ui::ListView::EventType::ON_SELECTED_ITEM_START) {
//        if (typeNum!=2) {
//            auto pListView = (ListView*)pSender;
//            int index = (int)pListView->getCurSelectedIndex();
//            int x =0;
//            for (auto it:(*GNS_PlayerMgr::getInstance()->getPlayProFit())) {
//                if (index==x) {
//                    CMD_CS_GetManToMachPlayProfit pGetPlayPro;
//                    pGetPlayPro._uplineCode=it._agentCode;
//                    pGetPlayPro._agentCode=(int)GNS_PlayerMgr::getInstance()->getMyUseridx();
//                    pGetPlayPro._gameId=1;
//                    pGetPlayPro._type=SendType+1;
//                    std::string TimeDate = StringUtils::format("%d-%02d-%02d",Year,Mon,Day);
//                    memcpy(pGetPlayPro._selDate, TimeDate.c_str(), 11);
//                    //_GameSock->sendData(MAIN_MSG_LOGIN, SUB_CS_GET_MANTOMACH_GAME_PROFIT, &pGetPlayPro, sizeof(pGetPlayPro));
//                    RecordlineCode=it._agentCode;
//                }
//                x++;
//            }
//        }
//    }
}

void GNS_PvEAllDate::SeleLayer(int LayerNum)
{
    pGNS_PvEAllDate->setVisible(false);
	pPveAllDateDaily->removeAllChildren();
	pPveAllDateDaily->setVisible(true);

    if (LayerNum==11) {  //人机对战总玩数据 （运营商登录，点击 代理商-》进入推广员 界面）
		pPveAllDateDaily->SetNodeName("PveAllDateDilyDate.csb");
        pPveAllDateDaily->SetProName(StringUtils::format("%d",RecordlineCode));
    }else if (LayerNum==12||LayerNum==21){
        pPveAllDateDaily->AccepPlayerDate();
		if (GNS_PlayerMgr::getInstance()->getUserLevel() == 1)
		{
			pPveAllDateDaily->pPvEAllDatePlay->SetProName(RecordlineCode, RecordlineCode);
		}	
    }
}
void GNS_PvEAllDate::ListViewBtn(cocos2d::Ref *pSender, ui::Widget::TouchEventType type)
{
    if (type==ui::Widget::TouchEventType::ENDED) {
		if (GNS_PlayerMgr::getInstance()->getUserLevel() < 2)
		{
			auto pButton = (Button*)pSender;
			int BtnTag = pButton->getTag();
			int x = 0;
			for (auto it : (*GNS_PlayerMgr::getInstance()->getPlayProFit())) {
				if (BtnTag - 100 == x) {
					GNS_PlayerMgr::getInstance()->getPlayProProFit()->clear();
					GNS_PlayerMgr::getInstance()->getPlayProPlayFit()->clear();
					CMD_CS_GetManToMachPlayProfit pGetPlayPro;
					pGetPlayPro._uplineCode = it._agentCode;
					pGetPlayPro._agentCode = (int)GNS_PlayerMgr::getInstance()->getMyUseridx();
					pGetPlayPro._gameId = GNS_PlayerMgr::getInstance()->getGameId();
					pGetPlayPro._type = SendType + 1;
					std::string TimeDate = StringUtils::format("%d-%02d-%02d", Year, Mon, Day);
					memcpy(pGetPlayPro._selDate, TimeDate.c_str(), 11);
					GnsClientFrame::getInstance()->sendMsg(0, MAIN_MSG_LOGIN, SUB_CS_GET_MANTOMACH_GAME_PROFIT, (char*)&pGetPlayPro, sizeof(pGetPlayPro));
					RecordlineCode = it._agentCode;
				}
				x++;
			}
		}
    }
    
}